package com.gc.tpas.service;

import com.gc.tpas.dao.QaSolutionDao;
import com.gc.tpas.dao.UserDCDao;
import com.gc.tpas.dao.UserDao;
import com.gc.tpas.entity.QaSolution;
import com.gc.tpas.entity.QaUser;
import com.gc.tpas.entity.QaUserDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2018/4/27 0027.
 */
@Transactional
@Service
public class UserService {
    @Autowired
    UserDao userDao;

    public QaUser login(String name, String pwd) {
        return userDao.findByLoginnameAndLoginpwd(name,pwd);
    }

    public QaUser register(QaUser u){
        return userDao.save(u);
    }

    public List<QaUser> getAllUser() {
        return userDao.findAll();
    }

    @Autowired
    UserDCDao userDCDao;

    public int exsitDC(int sid,int uid){
        List<QaUserDC> list = userDCDao.findBySidAndUid(sid,uid);
        if(list.size()>0){
            return list.get(0).getDingcai();
        }
        return -1;
    }
    @Autowired
    QaSolutionDao qaSolutionDao;

    public int dingcai(int sid,int dingcai,int uid){
        QaSolution s = qaSolutionDao.getOne(sid);
        QaUserDC userDC = new QaUserDC();
        userDC.setDingcai(dingcai);
        userDC.setSid(sid);
        userDC.setUid(uid);
        userDCDao.save(userDC);
        if(dingcai==1){
            qaSolutionDao.updateDing(s.getId(),s.getDing()+1);
            return s.getDing()+1;
        }else{
            qaSolutionDao.updateCai(s.getId(),s.getCai()+1);
            return s.getCai()+1;
        }
    }
}
